#include "..\..\Header\Arme\arme.h"

using namespace std;

Arme::Arme()
{
    m_degat = 0;
    m_t = 0;
    enTire = false;
    m_flip = false;
    m_nbtir = 0;
    m_repeat = 0;
    if(!m_bufferballe.loadFromFile("sounds/balle_simple.ogg"))
    {
        cout << "Impossible de charger le son des balles..." << endl;
    }
    else
    {
        m_soundballe.setBuffer(m_bufferballe);
    }
}
Arme::~Arme()
{
}
Arme::Arme(int degat, int nb_tir, int repeat, int precision)
{
    Option m_option;
    m_degat = 0;
    m_t = 0;
    enTire = false;
    m_flip = false;
    m_nbtir = 0;
    m_repeat = 0;
    if(!m_bufferballe.loadFromFile("sounds/balle_simple.ogg"))
    {
        cout << "Impossible de charger le son des balles..." << endl;
    }
    else
    {
        m_soundballe.setBuffer(m_bufferballe);
        m_soundballe.setVolume(m_option.GetVolume()/1.2);
    }
    SetDegat(degat,nb_tir,repeat,precision);
}
void Arme::SetFlip(bool flip)
{
    for(int i(0); i<m_tir.size(); i++)
        m_tir[i]->setScale(m_tir[i]->getScale().x * -1, m_tir[i]->getScale().y);
}
void Arme::SetDegat(int degat, int nb_tir, int repeat, int precision)
{
    m_degat = degat;
    m_nbtir = nb_tir;
    m_repeat = repeat;
    m_precision = precision;
}
void Arme::AfficherTir(sf::RenderWindow *app)
{
    if(enTire)
    {
        for(int i(0);i < m_tir.size();i++)
        {
            if(alive[i])
            {
                app->draw(*m_tir[i]);
            }
        }
    }
}
void Arme::Tirer(float angle, sf::Vector2f position, bool flip)
{
    srand(time(NULL));
    m_t = 0;
    m_position = position;
    int precision = m_precision;
    if(!m_image.loadFromFile("Picture/map/balle.png"))
    {
        cout << "Erreur dans le chargement de la balle." << endl;
    }
    for(int i(0);i<m_nbtir;i++)
    {
        m_tir.push_back(new sf::Sprite);
        m_tir[i]->setTexture(m_image);
        m_direction.push_back(sf::Vector2f(0,0));
        alive.push_back(false);
        int imprecit = (rand() % (int)(precision*2))-precision;
        float rotation = 0;
        float tmp_angle = 180-angle;
        if(flip)
        {
            rotation = angle-imprecit;
            m_direction[i].x = -acos((tmp_angle-imprecit)/180*Pi);
            if((angle+imprecit)/180*Pi > Pi)
                m_direction[i].y = asin((tmp_angle-imprecit)/180*Pi);
            else
                m_direction[i].y = -asin((tmp_angle-imprecit)/180*Pi);
        }
        else
        {
            rotation = angle+imprecit;
            m_direction[i].x = acos((angle+imprecit)/180*Pi);
            if((angle+imprecit)/180*Pi > Pi)
                m_direction[i].y = asin((angle+imprecit)/180*Pi);
            else
                m_direction[i].y = -asin((angle+imprecit)/180*Pi);
        }
        m_direction[i].x *= 10;
        m_direction[i].y *= 10;
        m_tir[i]->setRotation(rotation);
        m_tir[i]->setPosition(position);
    }
    enTire = true;
    during = true;
    SetFlip(flip);
}
void Arme::Deplacer(sf::Vector2f vent, Terrain *terre, Game *jeu, std::vector<sf::Sprite> unite)
{
    if(enTire)
    {
        if(during)
        {
            int i(0);
            for(i=0;i<alive.size();i++)
            {
                if(!alive[i] && m_t == i*m_repeat)
                {
                    m_soundballe.stop();
                    m_soundballe.play();
                    alive[i] = true;
                }
            }
            if(m_t >= i* alive.size())
                during = false;
        }
        int mort(0);
        int j(0);
        if(m_t > 100)
        {
            for(j=0;j<alive.size();j++)
                alive[j] = false;
        }
        for(j=0;j<alive.size();j++)
            if(!alive[j])
                mort++;

        if(mort == j)
        {
            jeu->finTire();
            int max(alive.size());
            for(int k(0);k<max;k++)
            {
                alive.pop_back();
                m_tir.pop_back();
                m_direction.pop_back();
                m_t = 0;
            }
        }
        m_t++;

        for(int i(0);i<m_tir.size();i++)
        {
            if(alive[i])
            {
                m_tir[i]->move(m_direction[i]);
                if(m_t > 5)
                {
                    if(m_tir[i]->getPosition().x>terre->GetTextureSize().x ||
                       m_tir[i]->getPosition().x<0 ||
                       m_tir[i]->getPosition().y>600 ||
                       m_tir[i]->getPosition().y<0 ||
                       terre->contact(m_tir[i]->getPosition()))
                    {
                        alive[i] = false;
                    }
                    for(int j(0);j<unite.size();j++)
                    {
                        if(CollisionBoiteSensible(*m_tir[i], unite[j]) != -1)
                        {
                            if(alive[i])
                                jeu->degat(4,m_tir[i]->getPosition(),m_degat, true, j, true);
                            alive[i] = false;
                        }
                    }
                }
            }
        }
    }
}
sf::Vector2f Arme::GetPosition()
{
    if(m_t < 50)
    {
        position = sf::Vector2f(0,0);
        for(int i(0);i<m_tir.size();i++)
        {
            if(alive[i])
            {
                position = m_tir[i]->getPosition();
                i = m_tir.size();
            }
        }
    }
    if(m_t > 10)
        return position;
    else
        return m_position;
}
Arme& Arme::operator=(const Arme& arme2)
{
    m_degat = arme2.m_degat;
    m_nbtir = arme2.m_nbtir;
    m_repeat = arme2.m_repeat;
    m_precision = arme2.m_precision;
}
int Arme::GetDegat()
{
    return m_degat;
}
